<template>
  <div
    :id="hasPreview ? info._id : `diy_${info._id}`"
    v-ui-ref="vSetRef"
    class="relative flex flex-col w-full overflow-hidden"
    :class="[
      'p-' + info.extension.padding.value,
      'mt-' + (info.extension.topMargin && info.extension.topMargin.value),
      'mr-' + (info.extension.rightMargin && info.extension.rightMargin.value),
      'mb-' + (info.extension.bottomMargin && info.extension.bottomMargin.value),
      'ml-' + (info.extension.leftMargin && info.extension.leftMargin.value),
    ]"
    :style="{
      height: `${info.extension.boxHeight.value}px`,
      backgroundImage: `url('${info.extension.backgroundImg.value}')`,
      backgroundRepeat: 'no-repeat',
      backgroundSize: `${info.extension.backgroundSize ? info.extension.backgroundSize.value : 'cover'}`,
      backgroundColor: 'rgba(0,0,0,0)',
    }"
  >
    <div
      v-if="info.extension.showBrage.value"
      class="border-t-2 border-l-2 absolute top-0 left-0 w-0.5 h-0.5 bg-transparent border-solid bradge"
      :style="`border-top-left-radius: ${info.extension.brageRotate.value}px;width: ${info.extension.brageWidth.value}px; height: ${info.extension.brageLine.value}px; border-color: ${info.extension.brageColorLeftTop.value}`"
    ></div>
    <div
      v-if="info.extension.showBrage.value"
      class="border-t-2 border-r-2 absolute right-0 top-0 w-0.5 h-0.5 bg-transparent border-solid bradge"
      :style="`border-top-right-radius: ${info.extension.brageRotate.value}px;width: ${info.extension.brageWidth.value}px; height: ${info.extension.brageLine.value}px; border-color: ${info.extension.brageColorRightTop.value}`"
    ></div>
    <div
      v-if="info.extension.showBrage.value"
      class="border-b-2 border-l-2 absolute left-0 bottom-0 w-0.5 h-0.5 bg-transparent border-solid bradge"
      :style="`border-bottom-left-radius: ${info.extension.brageRotate.value}px;width: ${info.extension.brageWidth.value}px; height: ${info.extension.brageLine.value}px; border-color: ${info.extension.brageColorLeftBottom.value}`"
    ></div>
    <div
      v-if="info.extension.showBrage.value"
      class="border-b-2 border-r-2 absolute right-0 bottom-0 w-0.5 h-0.5 bg-transparent border-solid bradge"
      :style="`border-bottom-right-radius: ${info.extension.brageRotate.value}px;width: ${info.extension.brageWidth.value}px; height: ${info.extension.brageLine.value}px; border-color: ${info.extension.brageColorRightBottom.value}`"
    ></div>
    <div
      class="flex align-center h-full"
      :class="['justify-' + info.extension.justifyAlign.value, 'items-' + info.extension.itemsAlign.value]"
      :style="{
        margin: `${info.extension.titleTopMargin.value}px ${info.extension.titleRightMargin.value}px ${info.extension.titleBottomMargin.value}px ${info.extension.titleLeftMargin.value}px`,
        padding: `${info.extension.titleTopPadding.value}px ${info.extension.titleRightPadding.value}px ${info.extension.titleBottomPadding.value}px ${info.extension.titleLeftPadding.value}px`,
        fontSize: `${info.extension.titleFontSize.value}px`,
        color: `${info.extension.titleColor.value}`,
        fontWeight: `${info.extension.titleFontWeight.value}`,
        backgroundColor: `${info.extension.titleBgColor.value}`,
        borderRadius: `${info.extension.titleRadius.value}px`,
        letterSpacing: `${info.extension.letterSpacing.value}px`,
        '--marqueeTime': info.extension.marqueeTime?.value + 's',
        // overflow: 'hidden',
        alignItems: info.extension.itemsAlign?.value,
      }"
      @click="clickBox"
    >
      <div
        v-if="info.extension.isMarquee?.value"
        :class="[info.extension.isMarquee?.value ? 'marquee w-full flex-none' : '']"
        v-html="
          myFormatte(
            info.extension.isDesign == undefined || info.extension.isDesign?.value ? formData?.themeText || info.extension.title.value : text
          )
        "
      ></div>
      <div
        :class="[info.extension.isMarquee?.value ? 'marquee w-full flex-none' : '']"
        v-html="
          myFormatte(
            info.extension.isDesign == undefined || info.extension.isDesign?.value ? formData?.themeText || info.extension.title.value : text
          )
        "
      ></div>
    </div>
  </div>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, RefsInjectMixin, ChartMixin, ConditionMixin } from '@ve/mixin'
  import cloneDeep from 'lodash/cloneDeep'
  import { getModelInfo } from '@ve/utils/getModelInfo'
  export default defineComponent({
    name: 'UiTitleCard',
    mixins: [UiMixin, RefsInjectMixin, ChartMixin, ConditionMixin],
    inject: {
      hasPreview: { default: false },
    },
    props: {
      info: {
        required: true,
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        infoComponent: {},
        inter: null,
        text: '',
        formData: {},
      }
    },
    watch: {
      info: {
        handler(val) {
          console.log(val, '变化了-----------')
          this.$nextTick(() => {
            this.infoComponent = cloneDeep(this.info)
            this.formData = getModelInfo(this, this.info._id)?.data?.formData
            this.getRequestData()
            this.inter && (clearInterval(this.inter), (this.inter = null))
            if (this.info.extension.requestSet && this.info.extension.requestSet.value && this.info.extension.requestSet.value.loopRequest) {
              this.inter = setInterval(() => {
                this.getRequestData()
              }, this.info.extension.requestSet.value.loopTime)
            }
          })
        },
        deep: true,
      },
      set: {
        handler(val) {
          console.log(val, '-------')
          this.$forceUpdate()
        },
        deep: true,
      },
      // getSelectModule: {
      //   handler(val, oldVal) {
      //     if (!oldVal.extension) {
      //       if (pieOption[this.info.chartTypeValue]) {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = false)
      //       } else {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = true)
      //       }
      //     }
      //   },
      //   deep: true,
      // },
    },
    created() {
      this.infoComponent = cloneDeep(this.info)
    },
    mounted() {
      this.$nextTick(() => {
        this.getRequestData()
        this.formData = getModelInfo(this, this.info._id)?.data?.formData
        this.inter && (clearInterval(this.inter), (this.inter = null))
        if (this.info.extension.requestSet.value.loopRequest) {
          this.inter = setInterval(() => {
            this.getRequestData()
          }, this.info.extension.requestSet.value.loopTime)
        } else {
          this.inter && (clearInterval(this.inter), (this.inter = null))
        }
      })
    },
    deactivated() {
      this.inter && clearInterval(this.inter)
    },
    beforeUnmount() {
      this.inter && clearInterval(this.inter)
    },
    methods: {
      clickBox() {
        if (!this.info.interaction?.length) {
          return
        }
        this.getInfo.data = { formData: this.info.extension.title.value }
        this.onClick()
      },
      myFormatte(str) {
        let con = ''
        if (!this.infoComponent.extension.formatte) {
          con = str
        } else {
          con = this.infoComponent.extension.formatte.value.replace(/\$\{value\}/g, () => {
            return str
          })
        }
        return con
      },
      getRequestData() {
        if (this.info.extension.isDesign?.value == false) {
          const data = cloneDeep(this.infoComponent.extension.requestSet.value)
          //
          if (this.info[this.info.getKey]) {
            data.data.push({
              key: this.info.getKey,
              value: this.info[this.info.getKey],
            })
          }
          this.$request(data, (res) => {
            this.infoComponent.extension.requestSet?.value.cardValue &&
              (this.text = cloneDeep(res)[0][this.infoComponent.extension.requestSet.value.cardValue])
          })
        }
      },
    },
  })
</script>
<style>
  .uiTitleCard {
    width: 100%;
    /* overflow: hidden; */
  }
  .marquee {
    animation: ani var(--marqueeTime);
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    width: 100%;
  }
  @keyframes ani {
    from {
      transform: translateX(0);
    }
    to {
      transform: translateX(100%);
    }
  }
</style>
